<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>multimap_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_labs_structs_multimap_test.js.html">multimap_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2012 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.labs.structs.MultimapTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.labs.structs.MultimapTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.labs.structs.Map&#39;);
<a name="line19"></a>goog.require(&#39;goog.labs.structs.Multimap&#39;);
<a name="line20"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line21"></a>
<a name="line22"></a>var map;
<a name="line23"></a>
<a name="line24"></a>
<a name="line25"></a>function setUp() {
<a name="line26"></a>  map = new goog.labs.structs.Multimap();
<a name="line27"></a>}
<a name="line28"></a>
<a name="line29"></a>
<a name="line30"></a>function testGetCountWithEmptyMultimap() {
<a name="line31"></a>  assertEquals(0, map.getCount());
<a name="line32"></a>  assertTrue(map.isEmpty());
<a name="line33"></a>}
<a name="line34"></a>
<a name="line35"></a>
<a name="line36"></a>function testClone() {
<a name="line37"></a>  map.add(&#39;k&#39;, &#39;v&#39;);
<a name="line38"></a>  map.addAllValues(&#39;k2&#39;, [&#39;v&#39;, &#39;v1&#39;, &#39;v2&#39;]);
<a name="line39"></a>
<a name="line40"></a>  var map2 = map.clone();
<a name="line41"></a>
<a name="line42"></a>  assertSameElements([&#39;v&#39;], map.get(&#39;k&#39;));
<a name="line43"></a>  assertSameElements([&#39;v&#39;, &#39;v1&#39;, &#39;v2&#39;], map.get(&#39;k2&#39;));
<a name="line44"></a>}
<a name="line45"></a>
<a name="line46"></a>
<a name="line47"></a>function testAdd() {
<a name="line48"></a>  map.add(&#39;key&#39;, &#39;v&#39;);
<a name="line49"></a>  assertEquals(1, map.getCount());
<a name="line50"></a>  map.add(&#39;key&#39;, &#39;v2&#39;);
<a name="line51"></a>  assertEquals(2, map.getCount());
<a name="line52"></a>  map.add(&#39;key&#39;, &#39;v3&#39;);
<a name="line53"></a>  assertEquals(3, map.getCount());
<a name="line54"></a>
<a name="line55"></a>  var values = map.get(&#39;key&#39;);
<a name="line56"></a>  assertEquals(3, values.length);
<a name="line57"></a>  assertContains(&#39;v&#39;, values);
<a name="line58"></a>  assertContains(&#39;v2&#39;, values);
<a name="line59"></a>  assertContains(&#39;v3&#39;, values);
<a name="line60"></a>}
<a name="line61"></a>
<a name="line62"></a>
<a name="line63"></a>function testAddValues() {
<a name="line64"></a>  map.addAllValues(&#39;key&#39;, [&#39;v&#39;, &#39;v2&#39;, &#39;v3&#39;]);
<a name="line65"></a>  assertSameElements([&#39;v&#39;, &#39;v2&#39;, &#39;v3&#39;], map.get(&#39;key&#39;));
<a name="line66"></a>
<a name="line67"></a>  map.add(&#39;key2&#39;, &#39;a&#39;);
<a name="line68"></a>  map.addAllValues(&#39;key2&#39;, [&#39;v&#39;, &#39;v2&#39;, &#39;v3&#39;]);
<a name="line69"></a>  assertSameElements([&#39;a&#39;, &#39;v&#39;, &#39;v2&#39;, &#39;v3&#39;], map.get(&#39;key2&#39;));
<a name="line70"></a>}
<a name="line71"></a>
<a name="line72"></a>
<a name="line73"></a>function testAddAllWithMultimap() {
<a name="line74"></a>  map.add(&#39;k&#39;, &#39;v&#39;);
<a name="line75"></a>  map.addAllValues(&#39;k2&#39;, [&#39;v&#39;, &#39;v1&#39;, &#39;v2&#39;]);
<a name="line76"></a>
<a name="line77"></a>  var map2 = new goog.labs.structs.Multimap();
<a name="line78"></a>  map2.add(&#39;k2&#39;, &#39;v&#39;);
<a name="line79"></a>  map2.addAllValues(&#39;k3&#39;, [&#39;a&#39;, &#39;a1&#39;, &#39;a2&#39;]);
<a name="line80"></a>
<a name="line81"></a>  map.addAllFromMultimap(map2);
<a name="line82"></a>  assertSameElements([&#39;v&#39;], map.get(&#39;k&#39;));
<a name="line83"></a>  assertSameElements([&#39;v&#39;, &#39;v1&#39;, &#39;v2&#39;, &#39;v&#39;], map.get(&#39;k2&#39;));
<a name="line84"></a>  assertSameElements([&#39;a&#39;, &#39;a1&#39;, &#39;a2&#39;], map.get(&#39;k3&#39;));
<a name="line85"></a>}
<a name="line86"></a>
<a name="line87"></a>
<a name="line88"></a>function testAddAllWithMap() {
<a name="line89"></a>  map.add(&#39;k&#39;, &#39;v&#39;);
<a name="line90"></a>  map.addAllValues(&#39;k2&#39;, [&#39;v&#39;, &#39;v1&#39;, &#39;v2&#39;]);
<a name="line91"></a>
<a name="line92"></a>  var map2 = new goog.labs.structs.Map();
<a name="line93"></a>  map2.set(&#39;k2&#39;, &#39;v&#39;);
<a name="line94"></a>  map2.set(&#39;k3&#39;, &#39;a&#39;);
<a name="line95"></a>
<a name="line96"></a>  map.addAllFromMultimap(map2);
<a name="line97"></a>  assertSameElements([&#39;v&#39;], map.get(&#39;k&#39;));
<a name="line98"></a>  assertSameElements([&#39;v&#39;, &#39;v1&#39;, &#39;v2&#39;, &#39;v&#39;], map.get(&#39;k2&#39;));
<a name="line99"></a>  assertSameElements([&#39;a&#39;], map.get(&#39;k3&#39;));
<a name="line100"></a>}
<a name="line101"></a>
<a name="line102"></a>
<a name="line103"></a>function testReplaceValues() {
<a name="line104"></a>  map.add(&#39;key&#39;, &#39;v&#39;);
<a name="line105"></a>  map.add(&#39;key&#39;, &#39;v2&#39;);
<a name="line106"></a>
<a name="line107"></a>  map.replaceValues(&#39;key&#39;, [0, 1, 2]);
<a name="line108"></a>  assertSameElements([0, 1, 2], map.get(&#39;key&#39;));
<a name="line109"></a>  assertEquals(3, map.getCount());
<a name="line110"></a>
<a name="line111"></a>  map.replaceValues(&#39;key&#39;, [&#39;v&#39;]);
<a name="line112"></a>  assertSameElements([&#39;v&#39;], map.get(&#39;key&#39;));
<a name="line113"></a>  assertEquals(1, map.getCount());
<a name="line114"></a>
<a name="line115"></a>  map.replaceValues(&#39;key&#39;, []);
<a name="line116"></a>  assertSameElements([], map.get(&#39;key&#39;));
<a name="line117"></a>  assertEquals(0, map.getCount());
<a name="line118"></a>}
<a name="line119"></a>
<a name="line120"></a>
<a name="line121"></a>function testRemove() {
<a name="line122"></a>  map.add(&#39;key&#39;, &#39;v&#39;);
<a name="line123"></a>  map.add(&#39;key&#39;, &#39;v2&#39;);
<a name="line124"></a>  map.add(&#39;key&#39;, &#39;v3&#39;);
<a name="line125"></a>
<a name="line126"></a>  assertTrue(map.remove(&#39;key&#39;, &#39;v&#39;));
<a name="line127"></a>  var values = map.get(&#39;key&#39;);
<a name="line128"></a>  assertEquals(2, map.getCount());
<a name="line129"></a>  assertEquals(2, values.length);
<a name="line130"></a>  assertContains(&#39;v2&#39;, values);
<a name="line131"></a>  assertContains(&#39;v3&#39;, values);
<a name="line132"></a>  assertFalse(map.remove(&#39;key&#39;, &#39;v&#39;));
<a name="line133"></a>
<a name="line134"></a>  assertTrue(map.remove(&#39;key&#39;, &#39;v2&#39;));
<a name="line135"></a>  values = map.get(&#39;key&#39;);
<a name="line136"></a>  assertEquals(1, map.getCount());
<a name="line137"></a>  assertEquals(1, values.length);
<a name="line138"></a>  assertContains(&#39;v3&#39;, values);
<a name="line139"></a>  assertFalse(map.remove(&#39;key&#39;, &#39;v2&#39;));
<a name="line140"></a>
<a name="line141"></a>  assertTrue(map.remove(&#39;key&#39;, &#39;v3&#39;));
<a name="line142"></a>  map.remove(&#39;key&#39;, &#39;v3&#39;);
<a name="line143"></a>  assertTrue(map.isEmpty());
<a name="line144"></a>  assertEquals(0, map.get(&#39;key&#39;).length);
<a name="line145"></a>  assertFalse(map.remove(&#39;key&#39;, &#39;v2&#39;));
<a name="line146"></a>}
<a name="line147"></a>
<a name="line148"></a>
<a name="line149"></a>function testRemoveWithNaN() {
<a name="line150"></a>  map.add(&#39;key&#39;, NaN);
<a name="line151"></a>  map.add(&#39;key&#39;, NaN);
<a name="line152"></a>
<a name="line153"></a>  assertTrue(map.remove(&#39;key&#39;, NaN));
<a name="line154"></a>  var values = map.get(&#39;key&#39;);
<a name="line155"></a>  assertEquals(1, values.length);
<a name="line156"></a>  assertTrue(isNaN(values[0]));
<a name="line157"></a>
<a name="line158"></a>  assertTrue(map.remove(&#39;key&#39;, NaN));
<a name="line159"></a>  assertEquals(0, map.get(&#39;key&#39;).length);
<a name="line160"></a>  assertFalse(map.remove(&#39;key&#39;, NaN));
<a name="line161"></a>}
<a name="line162"></a>
<a name="line163"></a>
<a name="line164"></a>function testRemoveWithNegativeZero() {
<a name="line165"></a>  map.add(&#39;key&#39;, 0);
<a name="line166"></a>  map.add(&#39;key&#39;, -0);
<a name="line167"></a>
<a name="line168"></a>  assertTrue(map.remove(&#39;key&#39;, -0));
<a name="line169"></a>  var values = map.get(&#39;key&#39;);
<a name="line170"></a>  assertEquals(1, values.length);
<a name="line171"></a>  assertTrue(1 / values[0] === 1 / 0);
<a name="line172"></a>  assertFalse(map.remove(&#39;key&#39;, -0));
<a name="line173"></a>
<a name="line174"></a>  map.add(&#39;key&#39;, -0);
<a name="line175"></a>
<a name="line176"></a>  assertTrue(map.remove(&#39;key&#39;, 0));
<a name="line177"></a>  var values = map.get(&#39;key&#39;);
<a name="line178"></a>  assertEquals(1, values.length);
<a name="line179"></a>  assertTrue(1 / values[0] === 1 / -0);
<a name="line180"></a>  assertFalse(map.remove(&#39;key&#39;, 0));
<a name="line181"></a>
<a name="line182"></a>  assertTrue(map.remove(&#39;key&#39;, -0));
<a name="line183"></a>  assertEquals(0, map.get(&#39;key&#39;).length);
<a name="line184"></a>}
<a name="line185"></a>
<a name="line186"></a>
<a name="line187"></a>function testRemoveAll() {
<a name="line188"></a>  map.add(&#39;key&#39;, &#39;v&#39;);
<a name="line189"></a>  map.add(&#39;key&#39;, &#39;v2&#39;);
<a name="line190"></a>  map.add(&#39;key&#39;, &#39;v3&#39;);
<a name="line191"></a>  map.add(&#39;key&#39;, &#39;v4&#39;);
<a name="line192"></a>  map.add(&#39;key2&#39;, &#39;v&#39;);
<a name="line193"></a>
<a name="line194"></a>  assertTrue(map.removeAll(&#39;key&#39;));
<a name="line195"></a>  assertSameElements([], map.get(&#39;key&#39;));
<a name="line196"></a>  assertSameElements([&#39;v&#39;], map.get(&#39;key2&#39;));
<a name="line197"></a>  assertFalse(map.removeAll(&#39;key&#39;));
<a name="line198"></a>  assertEquals(1, map.getCount());
<a name="line199"></a>
<a name="line200"></a>  assertTrue(map.removeAll(&#39;key2&#39;));
<a name="line201"></a>  assertSameElements([], map.get(&#39;key2&#39;));
<a name="line202"></a>  assertFalse(map.removeAll(&#39;key2&#39;));
<a name="line203"></a>  assertTrue(map.isEmpty());
<a name="line204"></a>}
<a name="line205"></a>
<a name="line206"></a>
<a name="line207"></a>function testAddWithDuplicateValue() {
<a name="line208"></a>  map.add(&#39;key&#39;, &#39;v&#39;);
<a name="line209"></a>  map.add(&#39;key&#39;, &#39;v&#39;);
<a name="line210"></a>  map.add(&#39;key&#39;, &#39;v&#39;);
<a name="line211"></a>  assertArrayEquals([&#39;v&#39;, &#39;v&#39;, &#39;v&#39;], map.get(&#39;key&#39;));
<a name="line212"></a>}
<a name="line213"></a>
<a name="line214"></a>
<a name="line215"></a>function testContainsEntry() {
<a name="line216"></a>  assertFalse(map.containsEntry(&#39;k&#39;, &#39;v&#39;));
<a name="line217"></a>  assertFalse(map.containsEntry(&#39;k&#39;, &#39;v2&#39;));
<a name="line218"></a>  assertFalse(map.containsEntry(&#39;k2&#39;, &#39;v&#39;));
<a name="line219"></a>
<a name="line220"></a>  map.add(&#39;k&#39;, &#39;v&#39;);
<a name="line221"></a>  assertTrue(map.containsEntry(&#39;k&#39;, &#39;v&#39;));
<a name="line222"></a>  assertFalse(map.containsEntry(&#39;k&#39;, &#39;v2&#39;));
<a name="line223"></a>  assertFalse(map.containsEntry(&#39;k2&#39;, &#39;v&#39;));
<a name="line224"></a>
<a name="line225"></a>  map.add(&#39;k&#39;, &#39;v2&#39;);
<a name="line226"></a>  assertTrue(map.containsEntry(&#39;k&#39;, &#39;v&#39;));
<a name="line227"></a>  assertTrue(map.containsEntry(&#39;k&#39;, &#39;v2&#39;));
<a name="line228"></a>  assertFalse(map.containsEntry(&#39;k2&#39;, &#39;v&#39;));
<a name="line229"></a>
<a name="line230"></a>  map.add(&#39;k2&#39;, &#39;v&#39;);
<a name="line231"></a>  assertTrue(map.containsEntry(&#39;k&#39;, &#39;v&#39;));
<a name="line232"></a>  assertTrue(map.containsEntry(&#39;k&#39;, &#39;v2&#39;));
<a name="line233"></a>  assertTrue(map.containsEntry(&#39;k2&#39;, &#39;v&#39;));
<a name="line234"></a>}
<a name="line235"></a>
<a name="line236"></a>
<a name="line237"></a>function testContainsKey() {
<a name="line238"></a>  assertFalse(map.containsKey(&#39;k&#39;));
<a name="line239"></a>  assertFalse(map.containsKey(&#39;k2&#39;));
<a name="line240"></a>
<a name="line241"></a>  map.add(&#39;k&#39;, &#39;v&#39;);
<a name="line242"></a>  assertTrue(map.containsKey(&#39;k&#39;));
<a name="line243"></a>  map.add(&#39;k2&#39;, &#39;v&#39;);
<a name="line244"></a>  assertTrue(map.containsKey(&#39;k2&#39;));
<a name="line245"></a>
<a name="line246"></a>  map.remove(&#39;k&#39;, &#39;v&#39;);
<a name="line247"></a>  assertFalse(map.containsKey(&#39;k&#39;));
<a name="line248"></a>  map.remove(&#39;k2&#39;, &#39;v&#39;);
<a name="line249"></a>  assertFalse(map.containsKey(&#39;k2&#39;));
<a name="line250"></a>}
<a name="line251"></a>
<a name="line252"></a>
<a name="line253"></a>function testContainsValue() {
<a name="line254"></a>  assertFalse(map.containsValue(&#39;v&#39;));
<a name="line255"></a>  assertFalse(map.containsValue(&#39;v2&#39;));
<a name="line256"></a>
<a name="line257"></a>  map.add(&#39;key&#39;, &#39;v&#39;);
<a name="line258"></a>  assertTrue(map.containsValue(&#39;v&#39;));
<a name="line259"></a>  map.add(&#39;key&#39;, &#39;v2&#39;);
<a name="line260"></a>  assertTrue(map.containsValue(&#39;v2&#39;));
<a name="line261"></a>}
<a name="line262"></a>
<a name="line263"></a>
<a name="line264"></a>function testGetEntries() {
<a name="line265"></a>  map.add(&#39;key&#39;, &#39;v&#39;);
<a name="line266"></a>  map.add(&#39;key&#39;, &#39;v2&#39;);
<a name="line267"></a>  map.add(&#39;key2&#39;, &#39;v3&#39;);
<a name="line268"></a>
<a name="line269"></a>  var entries = map.getEntries();
<a name="line270"></a>  assertEquals(3, entries.length);
<a name="line271"></a>  assertContainsEntry([&#39;key&#39;, &#39;v&#39;], entries);
<a name="line272"></a>  assertContainsEntry([&#39;key&#39;, &#39;v2&#39;], entries);
<a name="line273"></a>  assertContainsEntry([&#39;key2&#39;, &#39;v3&#39;], entries);
<a name="line274"></a>}
<a name="line275"></a>
<a name="line276"></a>
<a name="line277"></a>function testGetKeys() {
<a name="line278"></a>  map.add(&#39;key&#39;, &#39;v&#39;);
<a name="line279"></a>  map.add(&#39;key&#39;, &#39;v2&#39;);
<a name="line280"></a>  map.add(&#39;key2&#39;, &#39;v3&#39;);
<a name="line281"></a>  map.add(&#39;key3&#39;, &#39;v4&#39;);
<a name="line282"></a>  map.removeAll(&#39;key3&#39;);
<a name="line283"></a>
<a name="line284"></a>  assertSameElements([&#39;key&#39;, &#39;key2&#39;], map.getKeys());
<a name="line285"></a>}
<a name="line286"></a>
<a name="line287"></a>
<a name="line288"></a>function testGetKeys() {
<a name="line289"></a>  map.add(&#39;key&#39;, &#39;v&#39;);
<a name="line290"></a>  map.add(&#39;key&#39;, &#39;v2&#39;);
<a name="line291"></a>  map.add(&#39;key2&#39;, &#39;v2&#39;);
<a name="line292"></a>  map.add(&#39;key3&#39;, &#39;v4&#39;);
<a name="line293"></a>  map.removeAll(&#39;key3&#39;);
<a name="line294"></a>
<a name="line295"></a>  assertSameElements([&#39;v&#39;, &#39;v2&#39;, &#39;v2&#39;], map.getValues());
<a name="line296"></a>}
<a name="line297"></a>
<a name="line298"></a>
<a name="line299"></a>function testGetReturnsDefensiveCopyOfUnderlyingData() {
<a name="line300"></a>  map.add(&#39;key&#39;, &#39;v&#39;);
<a name="line301"></a>  map.add(&#39;key&#39;, &#39;v2&#39;);
<a name="line302"></a>  map.add(&#39;key&#39;, &#39;v3&#39;);
<a name="line303"></a>
<a name="line304"></a>  var values = map.get(&#39;key&#39;);
<a name="line305"></a>  values.push(&#39;v4&#39;);
<a name="line306"></a>  assertFalse(map.containsEntry(&#39;key&#39;, &#39;v4&#39;));
<a name="line307"></a>}
<a name="line308"></a>
<a name="line309"></a>
<a name="line310"></a>function testClear() {
<a name="line311"></a>  map.add(&#39;key&#39;, &#39;v&#39;);
<a name="line312"></a>  map.add(&#39;key&#39;, &#39;v2&#39;);
<a name="line313"></a>  map.add(&#39;key2&#39;, &#39;v3&#39;);
<a name="line314"></a>
<a name="line315"></a>  map.clear();
<a name="line316"></a>  assertTrue(map.isEmpty());
<a name="line317"></a>  assertSameElements([], map.getEntries());
<a name="line318"></a>}
<a name="line319"></a>
<a name="line320"></a>
<a name="line321"></a>function assertContainsEntry(entry, entryList) {
<a name="line322"></a>  for (var i = 0; i &lt; entryList.length; ++i) {
<a name="line323"></a>    if (entry[0] == entryList[i][0] &amp;&amp; entry[1] === entryList[i][1]) {
<a name="line324"></a>      return;
<a name="line325"></a>    }
<a name="line326"></a>  }
<a name="line327"></a>  fail(&#39;Did not find entry: &#39; + entry + &#39; in: &#39; + entryList);
<a name="line328"></a>}
</pre>


</body>
</html>
